package com.example.pclimb.ui.admin;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.pclimb.R;
import com.example.pclimb.databinding.ActivityAdminPanelBinding;
import com.example.pclimb.model.User;
import com.example.pclimb.ui.adapters.GuiderApplicationAdapter;
import com.example.pclimb.util.SharedPreferencesUtil;
import com.example.pclimb.viewmodel.UserViewModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 管理员面板活动
 */
public class AdminPanelActivity extends AppCompatActivity {

    private ActivityAdminPanelBinding binding;
    private UserViewModel userViewModel;
    private GuiderApplicationAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityAdminPanelBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        // 设置工具栏
        setSupportActionBar(binding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        binding.toolbar.setNavigationOnClickListener(v -> onBackPressed());

        // 初始化ViewModel
        userViewModel = new ViewModelProvider(this).get(UserViewModel.class);

        // 设置RecyclerView
        setupRecyclerView();

        // 验证是否是管理员
        int userId = SharedPreferencesUtil.getUserId(this);
        if (userId != -1) {
            userViewModel.getUserById(userId).observe(this, user -> {
                if (user == null || !user.isAdmin()) {
                    // 不是管理员，显示提示并返回
                    finish();
                } else {
                    // 是管理员，加载向导申请列表
                    loadGuideApplications();
                }
            });
        } else {
            // 未登录，返回登录页面
            finish();
        }
    }

    /**
     * 设置RecyclerView
     */
    private void setupRecyclerView() {
        adapter = new GuiderApplicationAdapter(this);
        binding.recyclerApplications.setLayoutManager(new LinearLayoutManager(this));
        binding.recyclerApplications.setAdapter(adapter);

        // 设置申请项点击事件
        adapter.setOnItemClickListener((application, position) -> {
            // 打开申请详情页面
            Intent intent = new Intent(this, GuiderApplicationDetailActivity.class);
            intent.putExtra("USER_ID", application.getId());
            startActivity(intent);
        });
    }

    /**
     * 加载向导申请列表
     */
    private void loadGuideApplications() {
        // 显示加载进度条
        binding.progressBar.setVisibility(View.VISIBLE);

        // 查询待审核的申请
        userViewModel.getUsersByApplicationStatus(User.APPLICATION_PENDING).observe(this, users -> {
            binding.progressBar.setVisibility(View.GONE);

            if (users != null && !users.isEmpty()) {
                // 显示申请列表
                binding.recyclerApplications.setVisibility(View.VISIBLE);
                binding.layoutNoApplications.setVisibility(View.GONE);
                adapter.setApplications(users);
            } else {
                // 显示无申请提示
                binding.recyclerApplications.setVisibility(View.GONE);
                binding.layoutNoApplications.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 页面恢复时重新加载申请列表
        loadGuideApplications();
    }
} 